package DAO;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import Main.DBMain;

public class AddCourse extends JFrame {
	private JPanel contentPane = new JPanel();
	private JLabel lab_Course, lab_teaName, lab_teaPwd;
	private JTextField tf_Course, tf_teaName, tf_teaPwd;
	private JButton btn_OK, btn_Exit;
	String teaName = null;
	String teaCourse = null;
	String teaPwd = null;
	String sql = "";

	public AddCourse() {
		setTitle("增加科目");
		setSize(400, 260);
		setLocationRelativeTo(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lab_Course = new JLabel("请输入新增科目名称：");
		lab_Course.setBounds(50, 20, 130, 30);
		contentPane.add(lab_Course);

		tf_Course = new JTextField(15);
		tf_Course.setBounds(190, 20, 150, 30);
		contentPane.add(tf_Course);

		lab_teaName = new JLabel("请输入执教老师姓名：");
		lab_teaName.setBounds(50, 70, 130, 30);
		contentPane.add(lab_teaName);

		tf_teaName = new JTextField(15);
		tf_teaName.setBounds(190, 70, 150, 30);
		contentPane.add(tf_teaName);

		lab_teaPwd = new JLabel("请输入新增老师密码：");
		lab_teaPwd.setBounds(50, 120, 130, 30);
		contentPane.add(lab_teaPwd);

		tf_teaPwd = new JTextField(15);
		tf_teaPwd.setBounds(190, 120, 150, 30);
		tf_teaPwd.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER) {
					addTeacher();
					add(teaCourse);
				}
			}
		});
		contentPane.add(tf_teaPwd);

		btn_OK = new JButton("确定");
		btn_OK.setFont(new Font("宋体", Font.BOLD, 13));
		btn_OK.setBounds(100, 170, 70, 40);
		btn_OK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addTeacher();
				add(teaCourse);
			}
		});
		contentPane.add(btn_OK);

		btn_Exit = new JButton("取消");
		btn_Exit.setFont(new Font("宋体", Font.BOLD, 13));
		btn_Exit.setBounds(240, 170, 70, 40);
		btn_Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		contentPane.add(btn_Exit);

		setVisible(true);

	}

	// 向学生表中添加一门新科目，放在英语科目之后，默认成绩均为0
	public void add(String sub) {
		Connection conn2 = null;
		Statement stmt = null;
		String sql1 = "ALTER TABLE stu_info ADD `" + sub + "` Double not null default 0 AFTER `classNo`"; // 添加字段语句
		try {
			// 连接数据库，进行增加字段
			conn2 = DBMain.getConnection();
			stmt = conn2.createStatement();
			stmt.executeUpdate(sql1);
			JOptionPane.showMessageDialog(null, "添加课程成功！");

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 向教师表中添加一个教师
	public void addTeacher() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		boolean result = false;
		teaCourse = tf_Course.getText().trim();
		teaName = tf_teaName.getText().trim();
		teaPwd = tf_teaPwd.getText().trim();
		if (!("".equals(teaName)) || !("".equals(teaCourse)) || !("".equals(teaPwd))) {
			result = teaIsExist(teaName, teaCourse);
		} else {
			JOptionPane.showMessageDialog(null, "输入信息不能为空！");
		}
		if (!result) {
			try {
				conn = DBMain.getConnection();
				sql = "insert into tea_info(teaName,teaCourse,teaPwd) values(?,?,?)"; // 向教师表中添加一名教师
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, teaName);
				pstmt.setString(2, teaCourse);
				pstmt.setString(3, teaPwd);

				pstmt.executeUpdate();
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, "添加教师失败！");
			}
		} else {
			JOptionPane.showMessageDialog(null, "该教师或者课程已存在！");
		}
	}

	// 教师是否已经存在，要求：姓名或者所教科目不能重复
	public boolean teaIsExist(String name, String course) {
		Connection conn1 = null;
		Statement stmt = null;
		ResultSet rs = null;
		boolean exist = false;
		try {
			conn1 = DBMain.getConnection();
			stmt = conn1.createStatement();
			rs = stmt.executeQuery(
					"select * from tea_info where (teaName='" + name + "' or teaCourse='" + course + "')");// 查询语句
			if (rs.next()) {
				exist = true;
			} else {
				exist = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return exist;
	}
}
